home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 5 / Gold Medal Software - Volume 5 (Gold Medal) (1995).iso / database / cff51b.arj / TREETEST.C < prev    next >
C/C++ Source or Header  |  1993-10-22  |  8KB  |  306 lines

  1. /* TREETEST.C */
  2. /* Copyright 1990, 1991, 1992, 1993 Norman D. Culver Ft. Lauderdale, FL */
  3. /*                    All Rights Reserved                                   */
  4.  
  5. #include <stdlib.h>
  6. #include "../cff.h"
  7.  
  8. #define CLOCKS_PER_SECOND 1000000L
  9.  
  10. /* TEST CHOICES */
  11. #define PFLAGS 0
  12. #define MEMFILE 1
  13. #define EXTDMEM 1
  14. #define CFFFILE 1
  15.  
  16. extern int errno;
  17. extern long clock();
  18. extern int rand();
  19. extern long tree_testcnt;
  20. extern long tree_bincnt;
  21.  
  22. static void tree_test(void *hd, void *hd1);
  23.  
  24. void
  25. main()
  26. {
  27. void *hxxx;
  28. void *huniq;
  29. void *hcfile;
  30. void *hsxxx;
  31. void *hsxxx1;
  32. void *hcxxx;
  33.  
  34.     cfport_settestflags(1);
  35.     cfinit("treetest",512,NULL);
  36.     cfprintf("B+ DIRECTORY keyed inserts, finds and deletes.\n");
  37.  
  38. #if MEMFILE
  39.     cfprintf("MEMORY FILE\n");
  40.     hxxx = cfopen("MEMORY/XXX",F_RDWR|F_CREAT,NULL);
  41.     if(!hxxx) {
  42.         cfprintf("ERROR: create XXX err=%d\n", errno);
  43.         exit(0);
  44.     }
  45.     huniq = cfopen("MEMORY/XXX",
  46.                     F_RDWR|F_CREAT|F_TEMP|F_UNIQ|F_SORTED|F_UNTAGGED, NULL);
  47.     if(!huniq) {
  48.         cfprintf("ERROR: create UNIQUE TEMP FILE err=%d\n", errno);
  49.         exit(0);
  50.     }
  51. #if PFLAGS
  52.     cfpflags("hxxx", hxxx);
  53.     cfpflags("huniq", huniq);
  54. #endif
  55.     tree_test(huniq, NULL);
  56. cfprintf("NOW CLOSING\n");
  57.     cfclose(huniq);
  58. #endif /* MEMFILE */
  59.  
  60. #if EXTDMEM
  61.     cfprintf("EXTDMEM FILE\n");
  62.     hsxxx = cfopen("EXTDMEM/XXX", F_RDWR|F_CREAT|F_SORTED|F_UNTAGGED, NULL);
  63.     if(!hsxxx) {
  64.         cfprintf("ERROR: create EXTDMEM/XXX err=%d\n", errno);
  65.         exit(0);
  66.     }
  67.     {/* Special case file, with preallocation of local bitmap */
  68.     OPNINFO info;
  69.         info.initial_entries = 0;
  70.         info.bitmap_prealloc = 240000;
  71.         info.data_prealloc = 0;
  72.         hsxxx1 = cfopen("EXTDMEM/XXX1",
  73.                         F_RDWR|F_CREAT|F_BITMAP|F_SORTED|F_TEMP|F_UNTAGGED,
  74.                         &info);
  75.         if(!hsxxx1) {
  76.             cfprintf("ERROR: create EXTDMEM/XXX1 err=%d\n", errno);
  77.             exit(0);
  78.         }
  79.     }
  80. #if PFLAGS
  81.     cfpflags("hsxxx", hsxxx);
  82.     cfpflags("hsxxx1", hsxxx1);
  83. #endif
  84.     cfsetlazy(hsxxx);
  85.     tree_test(hsxxx, hsxxx1);
  86. cfprintf("NOW CLOSING\n");
  87.     cfclose(hsxxx1);
  88.     cfclose(hsxxx);
  89.  
  90.     cfprintf("REOPEN EXTDMEM FILE\n");    
  91.     hsxxx = cfopen("EXTDMEM/XXX", F_RDWR, NULL);
  92.     if(!hsxxx) {
  93.         cfprintf("ERROR: reopen EXTDMEM:/XXX err=%d\n", errno);
  94.         exit(0);
  95.     }
  96.     cfsetlazy(hsxxx);
  97.     tree_test(hsxxx,NULL);
  98. cfprintf("NOW CLOSING\n");
  99.     cfunlink(hsxxx, NULL);
  100. #endif /* EXTDMEM */
  101.  
  102. #if CFFFILE
  103.     cfprintf("CFF DISK FILE\n");
  104.     hcfile = cfopen("testfile.cff", F_RDWR|F_CREAT, NULL);
  105.     if(!hcfile) {    
  106.         cfprintf("ERROR: create testfile.cff err=%d\n", errno);
  107.         exit(0);
  108.     }
  109.     hcxxx = cfopen("testfile.cff/XXX",
  110.                         F_RDWR|F_CREAT|F_SORTED|F_UNTAGGED, NULL);
  111.     if(!hcxxx) {    
  112.         cfprintf("ERROR: create testfile.cff/XXX err=%d\n", errno);
  113.         exit(0);
  114.     }
  115. #if 0
  116.     cfpflags("hcfile", hcfile);
  117.     cfpflags("hcxxx", hcxxx);
  118. #endif
  119.     cfsetlazy(hcxxx);
  120.     tree_test(hcxxx, NULL);
  121. cfprintf("NOW CLOSING\n");
  122.     cfclose(hcxxx);
  123.     cfclose(hcfile);
  124.  
  125.     cfprintf("REOPEN CFF DISK FILE\n");    
  126.     hcfile = cfopen("testfile.cff", F_RDWR, NULL);
  127.     if(!hcfile) {    
  128.         cfprintf("ERROR: reopen testfile.cff err=%d\n", errno);
  129.         exit(0);
  130.     }
  131.     hcxxx = cfopen("testfile.cff/XXX", F_RDWR, NULL);
  132.     if(!hcxxx) {    
  133.         cfprintf("ERROR: reopen testfile.cff/XXX err=%d\n", errno);
  134.         exit(0);
  135.     }
  136.     cfsetlazy(hcxxx);
  137.     tree_test(hcxxx,NULL);
  138. cfprintf("NOW CLOSING\n");
  139.     cfclose(hcxxx);
  140.     cfclose(hcfile);
  141.     cfunlink("testfile.cff");
  142. #endif /* CFF FILE */
  143.  
  144.     cfexit();
  145. } /* MAIN */
  146.  
  147. static Item keyval;
  148.  
  149. static void
  150. tree_test(void *hd, void *hd1)
  151. {
  152. long i, start, end, diff;
  153. long amount;
  154. long obtype = cfobtype(hd);
  155.  
  156.         if(obtype & OB_MEM)
  157.             amount = 10000;
  158.         else if(obtype & OB_CFILE)
  159.             amount = 10000;
  160.         else if(obtype & OB_SMEM)
  161.             amount = 10000;
  162.         else {
  163.             cfprintf("UNKNOWN OBTYPE: %x\n", obtype);
  164.             return;
  165.         }
  166.  
  167.         if(cfisnew(hd)) {
  168.         CAT cat;
  169.         int result;
  170.             start = clock();
  171.             tree_testcnt = tree_bincnt = 0;
  172.             for(i = 0; i < amount; ++i)
  173.             {/* Use keyhash just to jumble the keys, this may produce a dup */
  174.                 keyval.a0 = i+2;
  175.                 cfhash(&keyval, 4, &cat);
  176.  
  177.                 if((result = cfinsert(hd, &CATHASH, 4, &keyval)) < OK) {
  178.                     cfprintf("CFINSERT FAILED at i=%d result=%d\n", i, result);
  179.                     break;
  180.                 }
  181.             }            
  182.             end = clock();
  183.             diff = end - start;
  184.             diff /= CLOCKS_PER_SECOND/10;
  185.             if(diff == 0) diff = 1;
  186.             cfprintf("  KEYINSERTS PER SEC = %ld\n", (i*10) / diff);
  187.             cfprintf("  Node tests=%ld bincmp=%ld avg=%ld\n", 
  188.               tree_testcnt, tree_bincnt, tree_bincnt/tree_testcnt);
  189.         }
  190. #if 0
  191. print_entries(ccb);
  192. #endif
  193.         /* FINDITEM */
  194.         start = clock();
  195.         {
  196.         CAT cat;
  197.         int result;
  198.             tree_testcnt = tree_bincnt = 0;
  199.             for(i = 0; i < amount; ++i)
  200.             {/* Use keyhash just to jumble the keys, this may produce a dup */
  201.                 keyval.a0 = i+2;
  202.                 cfhash(&keyval, 4, &cat);
  203.  
  204.                 if((result = cffind_item(hd, &CATHASH, 4, &keyval)) != FOUND) {
  205.                     cfprintf("CFFINDITEM FAILED at i=%d result=%d val=%d\n",
  206.                         i, result, (int)keyval.a0);
  207.                     break;
  208.                 } else {
  209.                   if(keyval.a0 != i+2) {
  210.                     cfprintf("CFFINDITEM returned wrong value=%d should be %d\n",
  211.                             (int)keyval.a0, i+2);
  212.                     break;
  213.                   }
  214.                 }
  215.             }            
  216.             end = clock();
  217.             diff = end - start;
  218.             diff /= CLOCKS_PER_SECOND/10;
  219.             if(diff == 0) diff = 1;
  220.             cfprintf("  KEYFINDS PER SEC = %ld\n", (i*10) / diff);
  221.             cfprintf("  Node tests=%ld bincmp=%ld avg=%ld\n", 
  222.               tree_testcnt, tree_bincnt, tree_bincnt/tree_testcnt);
  223.         }
  224.  
  225.         /* DELETE */
  226.         start = clock();
  227.         {
  228.         CAT cat;
  229.         int result;
  230.             tree_testcnt = tree_bincnt = 0;
  231.             for(i = 0; i < amount/2; ++i)
  232.             {/* Use keyhash just to jumble the keys, this may produce a dup */
  233.                 keyval.a0 = i+2;
  234.                 cfhash(&keyval, 4, &cat);
  235.  
  236.                 if((result = cfdelete_item(hd, &CATHASH, 4, &keyval)) != OK) {
  237.                     cfprintf("CFDELETEITEM FAILED at i=%d result=%d val=%d\n",
  238.                         i, result, (int)keyval.a0);
  239.                     break;
  240.                 }
  241.             }            
  242.             end = clock();
  243.             diff = end - start;
  244.             diff /= CLOCKS_PER_SECOND/10;
  245.             if(diff == 0) diff = 1;
  246.             cfprintf("  KEYDELETEITEMS PER SEC = %ld\n", (i*10) / diff);
  247.             cfprintf("  Node tests=%ld bincmp=%ld avg=%ld\n", 
  248.               tree_testcnt, tree_bincnt, tree_bincnt/tree_testcnt);
  249.         }
  250.  
  251.         /* CHECK DELETE */
  252.         start = clock();
  253.         {
  254.         CAT cat;
  255.         int result;
  256.             tree_testcnt = tree_bincnt = 0;
  257.             for(i = 0; i < amount/2; ++i)
  258.             {/* Use keyhash just to jumble the keys, this may produce a dup */
  259.                 keyval.a0 = i+2;
  260.                 cfhash(&keyval, 4, &cat);
  261.  
  262.                 if((result = cffind_item(hd, &CATHASH, 4, &keyval)) >= FOUND){
  263.                     cfprintf("CFFINDDELETED FAILED at i=%d result=%d val=%d\n",
  264.                         i, result, (int)keyval.a0);
  265.                     break;
  266.                 }
  267.             }            
  268.             end = clock();
  269.             diff = end - start;
  270.             diff /= CLOCKS_PER_SECOND/10;
  271.             if(diff == 0) diff = 1;
  272.             cfprintf("  KEYNOFINDS PER SEC = %ld\n", (i*10) / diff);
  273.             cfprintf("  Node tests=%ld bincmp=%ld avg=%ld\n", 
  274.               tree_testcnt, tree_bincnt, tree_bincnt/tree_testcnt);
  275.         }
  276.  
  277.         /* REINSERT */
  278.         start = clock();
  279.         {
  280.         CAT cat;
  281.         int result;
  282.             tree_testcnt = tree_bincnt = 0;
  283.             for(i = 0; i < amount/2; ++i)
  284.             {/* Use keyhash just to jumble the keys, this may produce a dup */
  285.                 keyval.a0 = i+2;
  286.                 cfhash(&keyval, 4, &cat);
  287.  
  288.                 if((result = cfinsert(hd, &CATHASH, 4, &keyval)) < OK) {
  289.                     cfprintf("CFREINSERT FAILED at i=%d result=%d\n",
  290.                         i, result);
  291.                     break;
  292.                 }
  293.             }            
  294.             end = clock();
  295.             diff = end - start;
  296.             diff /= CLOCKS_PER_SECOND/10;
  297.             if(diff == 0) diff = 1;
  298.             cfprintf("  KEYREINSERTS PER SEC = %ld\n", (i*10) / diff);
  299.             cfprintf("  Node tests=%ld bincmp=%ld avg=%ld\n", 
  300.               tree_testcnt, tree_bincnt, tree_bincnt/tree_testcnt);
  301.         }
  302.  
  303.         cfprintf("  ALLOCATED:%lu USED:%lu PER ENTRY=%lu\n",
  304.           cfbytesalloc(hd), cfbytesused(hd), cfbytesused(hd)/amount);
  305. }
  306.